<META HTTP-EQUIV="Refresh" CONTENT="60; URL=showemployees.php">


<style>
table.buttons {font-size:30px;border-spacing:5px;width:100%;border-style:none;}
td.buttons {font-size:22px;font-family:arial;background-color:#D6E9F8;padding:5px;text-align:center;}
td.buttons2 {font-size:22px;font-family:arial;background-color:#ffaaaa;padding:5px;text-align:center;}
td.buttons3 {font-size:22px;font-family:arial;background-color:#7fff7f;padding:5px;text-align:center;}
td.buttons4 {font-size:22px;font-family:arial;background-color:#00ffaa;padding:5px;text-align:center;}
td.jobbuttons {font-size:20px;font-family:arial;background-color:#D6E9F8;padding:2px;text-align:center;}
input.bigin{font-size:62px;font-family:arial;background-color:#D6E9F8;padding:10px;text-align:center;}
a:link, a:visited, a:active	{	color: black;	text-decoration:none;	}
h1 {font-size:20px;font-family:arial}
h2 {font-size:20px;font-family:arial;color:red;border:solid;text-align:center}
</style>

<?php
require_once("include.php");
session_start();
(isset($_REQUEST["empid"])) ? $empid = $_REQUEST["empid"] : $empid = "NULL" ;
(isset($_REQUEST["processid"])) ? $processid = $_REQUEST["processid"] : $processid = "NULL" ;
(isset($_REQUEST["pgroupid"])) ? $pgroupid = $_REQUEST["pgroupid"] : $pgroupid = "NULL" ;
(isset($_REQUEST["jobid"])) ? $jobid = $_REQUEST["jobid"] : $jobid = "NULL" ;
(isset($_REQUEST["finish"])) ? $finish = $_REQUEST["finish"] : $finish = "NULL";
(isset($_REQUEST["finishjob"])) ? $finishjob = $_REQUEST["finishjob"] : $finishjob = "NULL";
(isset($_REQUEST["pin"])) ? $pin = $_REQUEST["pin"] : $pin = "NULL";
// Handler
$d =date("Y-m-d H:i:s");

if ($pin != "NULL") {
  $realpin = freadfield("employee",$empid,"pin");
  if ($realpin == $pin) {
   if ($finish != "NULL") {
    $stmt = "update work set endtime='$d' where empid='$empid' and starttime=endtime";
    sqlexec($stmt);
    showemps();
   } else {
    startedwork($empid);
    showprocess($empid);
   }
  } else {
   showpinfail();
  }
} elseif ($finish != "NULL") {
 getpid($empid,"finish");
} elseif ($finishjob != "NULL") {
  $stmt = "update work set endtime='$d' where empid='$empid' and starttime=endtime and processid = '$processid'";  
  sqlexec($stmt);  
  showprocess($empid);
} elseif ($jobid != "NULL") {
 showconfirm($empid,$processid,$pgroupid,$jobid);
} elseif ($pgroupid != "NULL") {
 showjobs($empid,$processid,$pgroupid);
} elseif ($processid != "NULL") {
 showpgroup($empid,$processid);
} elseif ($empid != "NULL") {
   $sql = "select * from work where empid='$empid' and starttime=endtime";
   $l = sqlreadarray($sql);
   $f = -1;
   if ($l) {
    foreach ($l as $key=>$value) {if ($value[6] == "ATT") {$f = $key;}}
   }
   if ($f != -1) {
     unset($l[$f]);
     if ($l) {
      alreadyworking($empid,$l);
     } else {
      showprocess($empid);
     }
   } else {
    getpid($empid,"start");
   } 
} else {
   showemps();
}

function alreadyworking($empid,$l) {
 foreach($l as $v) {
   echo "<h1>You are already doing some work<h1>";
   echo "<table>";
   echo "<tr><td>Process</td><td>$v[6]</td></tr>";
   echo "<tr><td>Product Group</td><td>$v[7]</td></tr>";
   echo "<tr><td>Job</td><td>$v[8]</td></tr>";
   echo "<tr><td>Started</td><td>".isotoout($v[9],1)."</td></tr>";
   echo "</table>";
   echo "<table border=2 cellspacing=15 class=buttons>";
   echo "<tr><td class=buttons><a href='?empid=$empid&processid=$v[6]&finishjob=true'>";
   echo "Finish this job";
   echo "</a></td></tr>";
   echo "<tr><td class=buttons2>";
   echo "<a href=showemployees.php>Back</a>";
   echo "</td></tr>";
   echo "</table>";
 }
}

function showpinfail() {
echo "<h1>You have entered the wrong PIN code</h1>";
echo "<table border=2 cellspacing=15 class=buttons><tr><td class=buttons2>";
echo "<a href=showemployees.php>Back</a>";
echo "</td></tr></table>";
}

function showemps() {
$l = sqlreadkeyarray("select itemid,name from employee where finished = 0 and department <> '' order by name");
$m = sqlreadkeyarray("select empid from work where endtime=starttime and processid <> 'ATT'");
$n = sqlreadkeyarray("select empid from work where endtime=starttime and processid = 'ATT'");
$o = sqlreadkeyarray("select empid from work where endtime=starttime and jobid = 'VARIOUS'");
print "<table border=2 cellspacing=7 class=buttons>";
print "<tr>";
$across = 0;
foreach ($l as $eid=>$name) {
  if ($across==3) {
   print "<tr>";
   $across = 0;
  }
  if (isset($o[$eid])) {
    // At work not on various
    print "<td class=buttons4>";
  } else if (isset($m[$eid])) {
   // Doing work
   print "<td class=buttons>";
  } else if (isset($n[$eid])) {
    // At work not on a job
    print "<td class=buttons3>";
  } else {
    // Not at work
    print "<td class=buttons2>";
  }
  print "<a href='?empid=$eid'>";
  print ucwords(strtolower($name[1]));
  print "</a>";
  print "</td>";
  $across +=1 ;
  if ($across==3) {
   print "</tr>";
  }
}
print "</tr>";
print "</table>";
print "<h1><a href=holidayschedule2010.htm>Holiday Schedule</a></h1>";

}

function showjobs($empid,$processid,$pgroupid) {
$empname = freadfield("employee",$empid,"name") ;
$empdet = freadfield("employee",$empid,"department");
echo "<h1>Choose a job for $empname</h1>";
if ($department == "Engineering") {
 $l = sqlreadarray("select * from jobs where discontinued <> 'Y' order by itemid");
} else {
 $l = sqlreadarray("select * from jobs where discontinued <> 'Y' and itemid not like 'NP%' order by itemid");
}

print "<table border=2 cellspacing=9 width=100% class=buttons><tr>";
$fitacross = 6;
$across = 0;
for ($i =0 ;$i < sizeof($l);$i++) {
  if ($across==$fitacross) {
   print "<tr>";
   $across = 0;
  }
  $id = $l[$i][0];
 ($id == "VARIOUS") ? $cla = "buttons2" : $cla = "jobbuttons";
  print "<td class=$cla>";
  print "<a href='?empid=$empid&processid=$processid&pgroupid=$pgroupid&jobid=$id'>";
  print $l[$i][0];
  print "</a>";
  print "</td>";
  $across +=1 ;
  if ($across==$fitacross) {
   print "</tr>";
  }
}
print "</tr>";
echo "<tr><td class=buttons2>";
echo "<a href=showemployees.php>Back</a>";
echo "</td></tr>";

print "</table>";
}

function showpgroup($empid,$processid) {
$empname = freadfield("employee",$empid,"name") ;
echo "<h1>Choose a Product Group for $empname</h1>";
$l = sqlreadarray("select * from pgroup where discontinued <> 'Y' order by name");
print "<table border=2 cellspacing=15 class=buttons><tr>";
$across = 0;
for ($i =0 ;$i < sizeof($l);$i++) {
  if ($across==3) {
   print "<tr>";
   $across = 0;
  }
  $id = $l[$i][0];
  ($id == "OTH") ? $cla = "buttons2" : $cla = "buttons";
  print "<td class=$cla>";
  print "<a href='?empid=$empid&processid=$processid&pgroupid=$id'>";
  print $l[$i][1];
  print "</a>";
  print "</td>";
  $across +=1 ;
  if ($across==3) {
   print "</tr>";
  }
}
print "</tr>";
echo "<tr><td class=buttons2>";
echo "<a href=showemployees.php>Back</a>";
echo "</td></tr>";

print "</table>";
}

function showprocess($empid) {
$empname = freadfield("employee",$empid,"name") ;

echo "<h1>Choose a process for $empname</h1>";
$l = sqlreadarray("select * from process where discontinued <> 'Y' and itemid <> 'ATT' order by name");
print "<table border=2 cellspacing=15 class=buttons><tr>";
$across = 0;
for ($i =0 ;$i < sizeof($l);$i++) {
  if ($across==3) {
   print "<tr>";
   $across = 0;
  }
  $id = $l[$i][0];
  ($id == "OTH") ? $cla = "buttons2" : $cla = "buttons";
  print "<td class=$cla>";
  print "<a href='?processid=$id&empid=$empid'>";
  print ucwords(strtolower($l[$i][1]));
  print "</a>";
  print "</td>";
  $across +=1 ;
  if ($across==3) {
   print "</tr>";
  }
}
print "</tr>";
echo "<tr><td class=buttons2><a href='?empid=$empid&finish=true'>";
echo "Clock off for today";
echo "</a></td></tr>";

echo "<tr><td class=buttons2>";
echo "<a href=showemployees.php>Back</a>";
echo "</td></tr>";

print "</table>";
}

function showconfirm($empid,$processid,$pgroupid,$jobid) {
 echo "<h1>Job confirmed<h1>";
 echo "<table>";
 echo "<tr><td>Employee</td><td>$empid</td></tr>";
 echo "<tr><td>Process</td><td>$processid</td></tr>";
 echo "<tr><td>Product Group</td><td>$pgroupid</td></tr>";
 echo "<tr><td>Job</td><td>$jobid</td></tr>";
 echo "<tr><td>Started</td><td>".date("H:i:s")."</td></tr>";
 echo "</table>";
 echo "<table border=2 cellspacing=15 class=buttons><tr><td class=buttons2>";
 echo "<a href=showemployees.php>Back</a>";
 echo "</td></tr></table>";
 $d =date("Y-m-d H:i:s");
 // Clear any other processes (in case user double-clocked)
  $stmt = "update work set endtime='$d' where empid='$empid' and processid <> 'ATT' and starttime=endtime";  
  sqlexec($stmt);  

 
 $header = "";
 $header[0] = "0";
 $header[1] = freadfield("employee",$empid,"name"); 
 $header[2] = "1";
 $header[3] = date("Y-m-d H:i:s");
 $header[4] = "Edit By";
 $header[5] = $empid;
 $header[6] = $processid;
 $header[7] = $pgroupid;
 $header[8] = $jobid; 
 $header[9] = $header[3];
 $header[10] = $header[3];
 $header[11] = "0-0-0 00:00:00";
 $header[12] = "0-0-0 00:00:00";
 $atts = 13;
 $stmt = "insert into work values (";
 for ($i=0;$i<$atts-1;$i++) {
  $stmt .= "'".$header[$i]."',";
 }
 $stmt .= "'".$header[$atts-1]."')"; 
 sqlexec($stmt);
} 

function startedwork($empid) {
 $header = "";
 $header[0] = "0";
 $header[1] = freadfield("employee",$empid,"name");
 $header[2] = "1";
 $header[3] = date("Y-m-d H:i:s");
 $header[4] = "Edit By";
 $header[5] = $empid;
 $header[6] = 'ATT' ;
 $header[7] = '' ;
 $header[8] = '' ;
 $header[9] = $header[3];
 $header[10] = $header[3];
 $header[11] = "0-0-0 00:00:00";
 $header[12] = "0-0-0 00:00:00";
 $atts = 13;
 $stmt = "insert into work values (";
 for ($i=0;$i<$atts-1;$i++) {
  $stmt .= "'".$header[$i]."',";
 }
 $stmt .= "'".$header[$atts-1]."')"; 
 sqlexec($stmt);
}

function getpid($empid,$onoroff) {
$name = freadfield("employee",$empid,"name") ;
echo "<h1>Please enter PIN for $name</h1>";
echo "<script>";
echo "function addv(v) {";
echo "document.form.pin.value = document.form.pin.value + v;";
echo "document.form.pinv.value = document.form.pinv.value + '*';";
echo "if (document.form.pin.value.length == 3) {";
echo "document.form.submit();";
echo "}";
echo "}";
echo "</script>";
echo "<div align=center><form name=form method=POST><input size=3 type=text name=pinv disabled class=bigin></div>";
echo "<input type=hidden name=pin></form>";
echo "<input type=hidden name=empid value=$empid></form>";
if ($onoroff == "start") {
 echo "<input type=hidden name=start value=$empid></form>";
} else {
 echo "<input type=hidden name=finish value=$empid></form>";
}
echo "<table border=2 cellspacing=15 class=buttons>";
echo "<tr><td class=buttons onclick=addv(1)>1</td><td class=buttons onclick=addv(2)>2</td><td class=buttons onclick=addv(3)>3</td></tr>";
echo "<tr><td class=buttons onclick=addv(4)>4</td><td class=buttons onclick=addv(5)>5</td><td class=buttons onclick=addv(6)>6</td></tr>";
echo "<tr><td class=buttons onclick=addv(7)>7</td><td class=buttons onclick=addv(8)>8</td><td class=buttons onclick=addv(9)>9</td></tr>";
echo "</table>";
echo "<h2>Don't forget to keep your process records up to date</h2>";
echo "<h2>Your name should be in a blue rectangle when you are working</h2>";
}
?>